package com.hhkj.study.admin.user.service.extend.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.study.admin.resource.entity.ResourceQuestionBank;
import com.hhkj.study.admin.resource.service.extend.ResourceQuestionBankExtendService;
import com.hhkj.study.admin.user.entity.UserExercise;
import com.hhkj.study.admin.user.model.podo.UserExerciseDo;
import com.hhkj.study.admin.user.model.podo.UserExercisePo;
import com.hhkj.study.admin.user.service.base.UserExerciseService;
import com.hhkj.study.admin.user.service.extend.UserExerciseExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.cont.SysConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
*
* @author SERVER
* @since 2024-09-06
*/
@Slf4j
@Service
public class UserExerciseExtendServiceImpl extends BaseExtendServiceImpl<UserExercise, UserExerciseDo, UserExercisePo, UserExerciseService> implements UserExerciseExtendService {

    @Resource
    private ResourceQuestionBankExtendService bankExtendService;

    @Override
    public boolean outsideSystemSave(UserExerciseDo userExercise) {
        LambdaQueryWrapper<UserExercise> userExerciseQuery = new LambdaQueryWrapper<>();
        userExerciseQuery.eq(UserExercise::getUserId, userExercise.getUserId()).eq(UserExercise::getCourseId, userExercise.getCourseId())
                .eq(UserExercise::getOrderItemId, userExercise.getOrderItemId()).eq(UserExercise::getBankId, userExercise.getBankId());
        List<UserExercise> userExerciseList = this.getService().list(userExerciseQuery);
        boolean isUpdate = false;
        if (CollectionUtil.isNotEmpty(userExerciseList)) {
            if (userExerciseList.size() == 1) {
                UserExercise firstUserExercise = CollectionUtil.getFirst(userExerciseList);
                if (ObjectUtil.isNull(firstUserExercise.getStartStudyTime())) {
                    isUpdate = true;
                    userExercise.setId(firstUserExercise.getId());
                }
            }
        }
        return saveOrUpdateUserExercise(userExercise, isUpdate);
    }

    /**
     * 更新和保存用户记录
     * @param userExercise
     * @param isUpdate
     */
    private boolean saveOrUpdateUserExercise(UserExerciseDo userExercise, boolean isUpdate) {
        ResourceQuestionBank questionBank = bankExtendService.getByIdNotNull(userExercise.getBankId());
        if (ObjectUtil.isNotNull(questionBank.getQuestionCount()) && userExercise.getExerciseCount() >= questionBank.getQuestionCount()) {
            userExercise.setExerciseCount(questionBank.getQuestionCount());
        }
        userExercise.setExerciseStatus(SysConst.Flag.IS.getFlag());
        if (isUpdate) {
            return this.getService().updateById(userExercise);
        } else {
            userExercise.setBankType(questionBank.getType());
            return this.getService().save(userExercise);
        }
    }
}
